﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.Event
{

    internal sealed class EventManager : GameFrameworkModule, IEventManager
    {
        public delegate void EventHandler(GameFrameworkParam param);

        Dictionary<string, EventHandler> m_DictEvent = new Dictionary<string, EventHandler>();



        /// <summary>
        /// 获取游戏框架模块优先级。
        /// </summary>
        /// <remarks>优先级较高的模块会优先轮询，并且关闭操作会后进行。</remarks>
        internal override int Priority
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 游戏框架模块轮询。
        /// </summary>
        /// <param name="elapseSeconds">逻辑流逝时间，以秒为单位。</param>
        /// <param name="realElapseSeconds">真实流逝时间，以秒为单位。</param>
        internal override void Update(float elapseSeconds, float realElapseSeconds)
        {

        }

        /// <summary>
        /// 关闭并清理游戏框架模块。
        /// </summary>
        internal override void Shutdown()
        {

        }

        /// <summary>
        /// 注册 事件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eventHandler"></param>
        public void RegisterEvent(string key, EventHandler eventHandler)
        {
            EventHandler evt;
            if (m_DictEvent.TryGetValue(key, out evt))
            {
                evt += eventHandler;
            }
            else
            {
                evt += eventHandler;
                m_DictEvent.Add( key, evt );
            }
        }

        /// <summary>
        /// 注销事件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="eventHandler"></param>
        public void UnRegisterEvent(string key, EventHandler eventHandler)
        {
            EventHandler evt;
            if (m_DictEvent.TryGetValue(key, out evt))
            {
                evt -= eventHandler;
            }
        }

        /// <summary>
        /// 执行事件
        /// </summary>
        /// <param name="key"></param>
        /// <param name="param"></param>
        public void DispatchEvent( string key, GameFrameworkParam param = null )
        {
            EventHandler eventHandler = null;
            if (m_DictEvent.TryGetValue(key, out eventHandler))
            {
                if (null != eventHandler) { eventHandler( param ); }
            }

            //Debug.LogError(string.Format( "", key ));
        }

        public void DispatchEvent<T>(string key, T p)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T>(p);
            DispatchEvent(key, param);
        }
        public void DispatchEvent<T1, T2>(string key, T1 p1, T2 p2)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T1, T2>(p1, p2);
            DispatchEvent(key, param);
        }
        public void DispatchEvent<T1,T2,T3>(string key, T1 p1, T2 p2, T3 p3)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T1, T2, T3>(p1, p2, p3);
            DispatchEvent(key, param);
        }
        public void DispatchEvent<T1, T2, T3,T4>(string key, T1 p1, T2 p2, T3 p3,T4 p4)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T1, T2, T3, T4>(p1, p2, p3, p4);
            DispatchEvent(key, param);
        }
        public void DispatchEvent<T1, T2, T3, T4,T5>(string key, T1 p1, T2 p2, T3 p3, T4 p4,T5 p5)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T1, T2, T3, T4, T5>(p1, p2, p3, p4, p5);
            DispatchEvent(key, param);
        }
        public void DispatchEvent<T1, T2, T3, T4,T5,T6>(string key, T1 p1, T2 p2, T3 p3, T4 p4, T5 p5,T6 p6)
        {
            GameFrameworkParam param = GameFrameworkParamFactory.CreateParam<T1, T2, T3, T4, T5, T6>(p1, p2, p3, p4, p5, p6);

            DispatchEvent(key, param);
        }

    }
}
